<?php
namespace App\Http\Controllers;
use App\Exceptions\ValidateException;
use App\Repository\Contracts\OrderCartInterface;
use App\Support\Helper\CommonHelper;
use Illuminate\Http\Request;
use Auth;

/**
 * @Author:: HuangYin
 * @DateTime: 2017-01-17 20:02
 *
 * @Description: 订单车
 */
class OrderCartController extends Controller
{
    /**
     * @var Request
     */
    protected $request;

    /**
     * @var OrderCartInterface
     */
    protected $orderCart;

    protected $uid;

    protected $storeId;

    /**
     * OrderCartController constructor.
     * @param Request $request
     * @param OrderCartInterface $orderCart
     */
    public function __construct(Request $request, OrderCartInterface $orderCart)
    {
        $this->request = $request;

        $this->orderCart = $orderCart;

        $this->uid = CommonHelper::getToken(Auth::user())['uid'];

        $this->storeId = CommonHelper::getToken(Auth::user())['storeId'];
    }

    /**
     * @return \Illuminate\Http\JsonResponse
     */
    public function index()
    {
        $list = $this->orderCart->lists($this->storeId);

        return $this->success($list);
    }

    /**
     * @return \Illuminate\Http\JsonResponse
     */
    public function created()
    {
        try {
            $data['goods_id'] = $this->request->input('goods_id');
            $data['store_id'] = $this->storeId;
            $id = $this->orderCart->created($data);

            return $this->success($id);
        } catch (ValidateException $e) {

            return $this->validateError($e);
        }
    }

    /**
     * @param $id
     * @return \Illuminate\Http\JsonResponse
     */
    public function updateCount($id)
    {
        $counts = $this->request->input('counts');
        try {
            $id = $this->orderCart->updateCount($id, $counts);

            return $this->success($id);
        } catch (ValidateException $e) {

            return $this->validateError($e);
        }
    }

    /**
     * @return \Illuminate\Http\JsonResponse
     */
    public function deleted()
    {
        $ids = $this->request->input('ids');
        try {
            $result = $this->orderCart->deleted($ids);

            return $this->success($result);
        } catch (ValidateException $e) {

            return $this->validateError($e);
        }
    }

    /**
     * @return \Illuminate\Http\JsonResponse|mixed
     */
    public function clear()
    {
        try {
            $result = $this->orderCart->clear();

            return $this->success($result);
        } catch (ValidateException $e) {

            return $this->validateError($e);
        }
    }
}